IPFS Tutorial

What is IPFS?

IPFS (the InterPlanetary File System) is a recent Web 3.0 protocol developed by Protocol Labs to help decentralize and distribute the internet.

A distributed internet is a web where nearly every user’s machine acts as a host of content rather than just a recipient of that content. This is desirable for a number of reasons:

And so on.

How Does It Work?

IPFS’s secret ingredient is content based addressing rather than HTTP’s location based addressing.

So instead of retrieving a file using some server’s IP address and hoping it gives you what you’re looking for, you can search for that file on the entire network using its content identifier, or CID; so every link to that file looks exactly the same, even if it’s stored on hundreds of different nodes all across the network.

A file’s CID is based on the data stored in the file itself; so if the file changes at all, its link changes, too. This way you can be sure that you’re receiving what you’ve asked for.

Getting Started

In order to browse content on IPFS like you would the regular internet, you’re going to need a compatible browser. Brave and Opera both have native support.

If you’re on a Chromium-based browser, you should be able to use IPFS using the Chrome Extension IPFS Companion.

Here are some links you can test to see if it works.

Those last two links are Ethereum domains, so they may not work depending on whether the browser requires an extension like Metamask at the time of reading.

Here’s the Content ID of this website’s homepage stored on IPFS:


Publishing Content

If you’re using Brave, you can publish files to IPFS using the extension, but we’re cool guys so we’re going to use the command line instead.

On Linux, go to your package manager and search for IPFS. The program we’re using is called go-ipfs.

If you’re on Windows or something else try to install it directly from their GitHub or whatever you guys do, I don’t really know I don’t think in proprietary software anymore.

Start up a terminal and navigate to a folder where the file you want to publish is. Once you find your examplefile.txt, type this command:

ipfs add examplefile.txt

The output should look something like this:

added QmX2PxhzBKVqnPzx5xAdoJVeSAq5dhKAAXqhPJf7tTQjy5 examplefile.txt

You may be tempted to type your hash into your browser to view the file you just published, but it won’t work yet. I’ll explain in the next section how to view content you publish from your browser or another computer, but for now we’re sticking to the terminal.

If you want to view your examplefile.txt that you just published, use cat:

ipfs cat QmX2PxhzBKVqnPzx5xAdoJVeSAq5dhKAAXqhPJf7tTQjy5

and the output of your file will print to your terminal window.

To add an entire directory of files to IPFS, navigate to that ~/folder and use the -r option to add all the files recursively.

ipfs add -r ~/folder

In the output, you’ll see a hash for each of the files you added in that folder, and you’ll also see a hash for the folder itself.

Entire folders and their contents can be added to IPFS.

Hosting Content

All we’ve done up to this point is generate the content addresses of files we want to add to the network, but that doesn’t mean those files are actually available on the network yet. In order to make our content accessible to other users, we have to host it for them.

In IPFS, hosting content is called pinning.

If you’ve got IPFS working in your browser, try this:

First, add whatever examplefile.txt you want to the network using the commands above. Then, pin the content to your local node using this command:

ipfs pin add QmX2PxhzBKVqnPzx5xAdoJVeSAq5dhKAAXqhPJf7tTQjy5

Next, start up your local node by starting the IPFS daemon.

ipfs daemon

Now your computer is part of the network; it can both host and retrieve content from other nodes on the internet.

To test this, open your IPFS capable browser and paste the content identifier into the search bar like this ipfs://QmX2PxhzBKVqnPzx5xAdoJVeSAq5dhKAAXqhPJf7tTQjy5 and you should be able to view the text of that file in the browser.

You can also do this with the CID of the folder you added; this will let you navigate the contents of that directory in your browser.

And that’s it. As long as a daemon is running somewhere with your content on it, you’ll be able to access your files from anywhere in the world using only their CIDs.

But what if you’re pinning something like a website directory whose content will change over time and you need a link to it that won’t change every time you edit the website?

There’s a solution.

Follow me to my tutorial on IPNS, the InterPlanetary Name System.