Visual Studio Code - Fix Hash Sum Mismatch

Yesterday, I did $ sudo apt update and I ended up getting the following error -

Err:25 http://packages.microsoft.com/repos/vscode stable/main amd64 Packages
Hash Sum mismatch

To be honest, I had no idea what does this error even stands for, so I searched for ubuntu apt-update hash sum mismatch fix on google, and got the following results -

One of the users wrote that the easiest fix would be -

$ sudo apt clean
$ sudo apt update

Another fix, according to a user, was to remove the contents of /var/lib/apt/lists and run sudo apt update.

A comment on that answer suggested -

If you remove all files, you have to download them again. You can just remove the invalid file to make this process faster.

Part 1

Now, I had two options to fix the issue, but for some reasons, I didn’t want to remove the contents of /var/lib/apt/lists, so I started with -

sudo apt clean
sudo apt autoclean
sudo apt update

Unfortunately, the solution didn’t work. I was still getting the hash sum mismatch error.

Part 2

Removing the contents of /var/lib/apt/lists.

Since the source of problem was Visual Studio Code, it was completely unnecessary to remove all the files. So, I just had to delete the files which were related to vscode -

cd /var/lib/apt/lists
ls | grep vs
packages.microsoft.com_repos_vscode_dists_stable_InRelease
packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages
sudo su # or just su
cd /var/lib/apt/lists
rm packages.microsoft.com_repos_vscode_dists_stable_InRelease
rm packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages
sudo apt clean && sudo apt autoclean && sudo apt remove && 
sudo apt autoremove && sudo apt update # I know. Please don't judge.

ISSUE

The problem didn’t disappear.

It was still getting the same error while updating. Only this time, I read the error carefully.

Part 3

This part is all about hit and trial, and trusting my instincts alongside!

Failed to fetch http://packages.microsoft.com/repos/vscode/dists/stable/main/binary-amd64/Packages.bz2

The above error made me check if Packages.bz2 file exists in /var/lib/apt/lists. So, I checked -

ls /var/lib/apt/lists | grep Packages.bz2

Result - Nothing. Packages.bz2 was missing! Good News?

My initial thought - Add Packages.bz2 to /var/lib/apt/lists and run sudo apt update.

Problem - I had already deleted the two files. -

packages.microsoft.com_repos_vscode_dists_stable_InRelease
packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages

How to get those files back?

The files available in /var/lib/apt/lists are in the form of site.com_foo_bar_buzz. Now, if you replace _ with /, you’ll get -

site.com/foo/bar/buzz

So,

packages.microsoft.com_repos_vscode_dists_stable_InRelease

will become -

packages.microsoft.com/repos/vscode/dists/stable/InRelease

Since two of the files related to VSCode were already gone, I had to download them and put them back to their place. Kudos to history.

history | grep packages.microsoft

I also needed the Packages.bz2 file, so I grabbed the downloadable link from the error message.

http://packages.microsoft.com/repos/vscode/dists/stable/InRelease
http://packages.microsoft.com/repos/vscode/dists/stable/main_binary-amd64_Packages
http://packages.microsoft.com/repos/vscode/dists/stable/main/binary-amd64/Packages.bz2
packages.microsoft.com_repos_vscode_dists_stable_InRelease
packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages
packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages.bz2

Now, move those files to /var/lib/apt/lists

sudo su
mv packages.microsoft.com_repos_vscode_dists_stable_InRelease /var/lib/apt/lists
mv packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages /var/lib/apt/lists
mv packages.microsoft.com_repos_vscode_dists_stable_main_binary-amd64_Packages.bz2 /var/lib/apt/lists

Run sudo apt update.

This fixed the whole Hash Sum Mismatch error for me!.

I had no idea if it would work or not. I attempted to solve a problem, and everything went fine.

Links